
from fastapi import FastAPI,Response,Request

app = FastAPI()

from io import BytesIO
import uvicorn

from fapiaopdf import invoice_show
from fapiaoofd import InvoiceOFD
import argparse
#python -m nuitka --output-dir=out --show-progress --mingw64 --lto --onefile --standalone --remove-output --remove-output fapiaowebapp.py 文件打包

@app.get("/")
def read_root():
    byte_io = BytesIO()
    test_item = {
        'KPLX':'31',
        'GGFMC':'购货方名称',
        'GGFSBH':'91320115MA27HYXA46',
        'XHFMC':'上海黄浦区大众养老院',
        'XHFSBH':'91310000767934044Y',
        'FPHM':'24312000000115629364',
        'KPRQ':'2024年04月25日',
        'JSHJ':'800000.00',
        'HJ':'1111111111.00',
        'HJSE':'222222222.00',
        'KPR':'彭清艳',
        'BZ':'''ddddddddddddddddddddddddddddd打撒打撒打撒十大sssssssssssssssd大苏打实打实的''',
        'datas':[
            {
                'ItemName':'*人力资源服务*劳务',
                'SpecMod':'规格型号1',
                'MeaUnits':'单位',
                'Quantity':'2.00',
                'UnPrice':'200000.00',
                'Amount':'400000.00',
                'TaxRate':'0.06',
                'ComTaxAm':'24000.00'
            },
            {
                'ItemName':'*人力资源服务*劳务',
                'SpecMod':'规格型号2',
                'MeaUnits':'单位',
                'Quantity':'2.00',
                'UnPrice':'200000.00',
                'Amount':'400000.00',
                'TaxRate':'0.06',
                'ComTaxAm':'24000.00'
            },
        ]
    }
    c = invoice_show(test_item, byte_io,"shanghai")
    c.save()
    pdf = byte_io.getvalue()
    byte_io.close()
    return Response(content=pdf, media_type="application/pdf")

@app.post("/invoicepdf_show/")
async def invoicepdf_show(request: Request):
    item = await request.json()
    #print(item)
    byte_io = BytesIO()
    
    c = invoice_show(item, byte_io,item['zhangpng'])
    c.save()
    pdf = byte_io.getvalue()
    byte_io.close()
    return Response(content=pdf, media_type="application/pdf")

@app.post("/invoiceofd_show/")
async def invoiceofd_show(request: Request):
    item = await request.json()

    ofd = InvoiceOFD().makeofd(item,item['zhangpng'])
    
    ofd_f = ofd.read()

    return Response(content=ofd_f, media_type="application/ofd")
    
if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    # 添加一个可选参数
    parser.add_argument("--port", help="端口号")
    # 解析命令行参数
    args = parser.parse_args()
    port = int(args.port) if args.port else 8121
     
    uvicorn.run(app, host="0.0.0.0", port=port)
